﻿Type.registerNamespace('TallComponents');

TallComponents.BookmarksList = function (element) {
   ///<summary>Displays a list of PDF bookmarks</summary>
   TallComponents.BookmarksList.initializeBase(this, [element]);
   this._baseUrl = null;
   this._targetControlId = null;
   this._targetControl = null;
   this._storageProviderName = null;
   this._activeCssClass = null;
};

//
// Create the prototype for the control.
//
TallComponents.BookmarksList.prototype = {
   initialize: function () {
      ///<summary>Initializes the control.</summary>
      ///<remarks>The initialize method initializes the control and sets the base <see cref="M:J#Sys.Component.isInitialized"/> property to true.</remarks>
      TallComponents.BookmarksList.callBaseMethod(this, "initialize");

      if (!this._hookupViewer()) {
         this._appLoadDelegate = Function.createDelegate(this, this._appLoad);
         Sys.Application.add_load(this._appLoadDelegate);
      }
   },

   dispose: function () {
      ///<summary>Removes the current control from the application.</summary>
      ///<remarks>The dispose method releases all resources from the <see cref="T:J#Sys.UI.Control"/> object, 
      ///unbinds it from its associated HTML Document Object Model (DOM) element, 
      ///and unregisters it from the application.</remarks>
      if (this._appLoadDelegate) { Sys.Application.remove_load(this._appLoadDelegate); }
      TallComponents.BookmarksList.callBaseMethod(this, 'dispose');
   },

   _appLoad: function (sender, args) {
      Sys.Application.remove_load(this._appLoadDelegate);
      this._appLoadDelegate = null;
      this._hookupViewer();
   },

   _hookupViewer: function () {
      this._targetControl = $find(this._targetControlId);

      if (this._targetControl) {

         var that = this;
         for (var i = 0; i < this._bookmarkDataList.length; i++) {
            //find the element with the id equal to AId
            var aElement = document.getElementById(this._bookmarkDataList[i].AId);

            if (aElement) {
               //assign an action on 'onclick' event for this element
               //this action causes the targetControl go to the specified page
               //with the specified Zoom and specified position
               aElement.onclick = function (x) {
                  return function () {
                     that.set_pageIndex(x);
                     if (that._activeCssClass) {
                        var elt = document.getElementById(x.AId);
                        that._setActiveStyle(elt, that._activeCssClass);
                     }
                  };
               } (this._bookmarkDataList[i]);
            }
            else {
               alert(String.format("The link with the ID:{0} could not be found", this._bookmarkDataList[i].AId));
            }
         }
         return true;
      }
      setTimeout(Function.createDelegate(this, this._hookupViewer), 500);
      return false;
   },

   set_pageIndex: function (goToData) {
      var zoom = (!(isNaN(goToData.Zoom) || goToData.Zoom == null || goToData.Zoom == '')) ? goToData.Zoom : null;
      this._targetControl.goTo(goToData.Page, zoom, { x: goToData.PosX, y: goToData.PosY });
   },

   _setActiveStyle: function (element, activeCssClass) {
      //clear active style
      for (var i = 0; i < this._bookmarkDataList.length; i++) {
         var e = document.getElementById(this._bookmarkDataList[i].AId);
         Sys.UI.DomElement.removeCssClass(e, this._activeCssClass);
      }
      //add active style
      Sys.UI.DomElement.addCssClass(element, this._activeCssClass);
   },

   get_targetControlId: function () {
      ///<summary>Gets or sets the control id of the associated PdfViewer control.</summary>
      ///<remarks>Setting this value after the control is initialized has no effect.</remarks>
      ///<value type="String" mayBeNull="false">The control id of the associated PdfViewer control.</value>
      return this._targetControlId;
   },

   set_targetControlId: function (value) {
      if (this._targetControlId != value) {
         this._targetControlId = value;
         this.raisePropertyChanged('targetControlId');
      }
   },

   get_activeCssClass: function () {
      /// <summary>
      /// Gets or sets the CSS class to be applied to the selected bookmark.
      /// </summary>
      /// <value type="String">The CSS class for the selected bookmark.</value>
      return this._activeCssClass;
   },

   set_activeCssClass: function (value) {
      if (this._activeCssClass != value) {
         this._activeCssClass = value;
         this.raisePropertyChanged('activeCssClass');
      }
   },

   get_bookmarkDataList: function () {
      return this._bookmarkDataList;
   },

   set_bookmarkDataList: function (value) {
      this._bookmarkDataList = value;
   },

   get_storageProviderName: function () {
      /// <summary>
      /// Gets or sets the name of the storage provider.
      /// </summary>
      return this._storageProviderName;
   },

   set_storageProviderName: function (value) {
      this._storageProviderName = value;
   }
};

// Optional descriptor for JSON serialization.
TallComponents.BookmarksList.descriptor = {
   properties: [
                 { name: "targetControlId", type: String },
                 { name: "activeCssClass", type: String },
                 { name: "storageProviderName", type: String },
                 { name: "bookmarkDataList", type: Object}]
};
// Register the class as a type that inherits from Sys.UI.Control.
TallComponents.BookmarksList.registerClass('TallComponents.BookmarksList', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') { Sys.Application.notifyScriptLoaded(); }